<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
    
    <style>
    #viewer{
			position: relative;
			padding: 0 0px;
			top: -70px;
			font-size: 54px;
			line-height: 60px;
		}
    </style>
  <div class="easyui-panel" style="padding:10px 10px">
  <div style="padding:10px 30px 20px 30px">
  		<form id="keyPairForm" method="post">
  		<input type="hidden" class="easyui-textbox" name="id" >
	    	<table cellpadding="5">
	    		<tr>
	    			<td style="width:150px;">Access Key:</td>
	    			<td style="width:300px;">
						<input id="accessKey" class="easyui-textbox" name="accessKey" prompt="Access Key" data-options="required:true,validType:'maxLength[48]'" iconWidth="28" style="width:100%;height:34px;padding:10px;">
	    			</td>
	    		</tr>
	    		<tr>
	    			<td style="width:150px;">Secret Key:</td>
	    			<td style="width:300px;">
						<input id="secretKey" class="easyui-passwordbox" name="secretKey" prompt="Secret Key" data-options="required:true,validType:'fixedLength[32]'" iconWidth="28" style="width:100%;height:34px;padding:10px">
	    			</td>
	    		</tr>
	    	</table>
	    </form>
	     	<div style="text-align:center;padding:5px;margin-top:50px;">
	    		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="keypair_test()">测试连接 </a>
	    		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="keypair_submitForm()">保   存</a>
	    		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="keypair_cancel()">取  消</a>
	    	</div>
		 </div>
	</div>
<div id="viewer"></div>
	
<script type="text/javascript">
$(function(){
	$.getJSON("../keyPair/getKeyPair",function(res){
		if(res.status == 200){
			var data = res.data;
			if(data){
				$('#keyPairForm').form('load',{
					accessKey:data.accessKey,
					secretKey:data.secretKey,
					id:data.id
				});
			}
		}else{
			$.messager.alert('提示',res.message,'info');
		}
	})
})


$.extend($.fn.validatebox.defaults.rules, {    
    fixedLength: {    
        validator: function(value, param){    
            return value.length == param[0];    
        },    
        message: '请输入{0}位正确的值'   
    }
}); 
//保存密钥对
function keypair_submitForm(){
	$.messager.progress();
	$('#keyPairForm').form('submit',{
		url:"../keyPair/save",
		onSubmit:function(){
			var isValid =  $(this).form('enableValidation').form('validate');
			if(!isValid){
				$.messager.progress('close');	// 如果表单是无效的则隐藏进度条
			}
			return isValid;
		},
		success: function(res){
			$.messager.progress('close');
			res = JSON.parse(res);
	         if(res.status == 200){
	        	 $.messager.alert('提示',"保存成功！",'info',function(){
		        	 $("#window_keyPair").window('close'); 
	        	 });
	         }else{
	        	 $.messager.alert('提示',res.message,'info');
	         }
	    } 
	});
}
//测试密钥对
function keypair_test(){
	$.messager.progress();
	$('#keyPairForm').form('submit',{
		url:"../keyPair/testKeyPair",
		onSubmit:function(){
			var isValid =  $(this).form('enableValidation').form('validate');
			if(!isValid){
				$.messager.progress('close');	// 如果表单是无效的则隐藏进度条
			}
			return isValid;
		},
		success: function(res){
			$.messager.progress('close');
			res = JSON.parse(res);
	         if(res.status == 200){
	        	 $.messager.alert('提示',"Ok!",'info');
	         }else{
	        	 $.messager.alert('错误',res.message,'info');
	         }
	    } 
	});
}
//关闭窗口
function keypair_cancel(){
	$("#window_keyPair").window('close'); 
}
</script>
